package lion.framework.core.web.views;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;

import lion.framework.core.util.AppPaths;
import lion.framework.core.web.anno.ViewType;
import lion.framework.core.web.exception.WebException;
import lion.framework.core.web.path.ParameterizedPath;
import lion.framework.core.web.request.RequestModel;

/**
 * WebContext Resource 将指定路径的资源返回
 *
 * <pre>
 * time   2013 2013-5-14 下午05:17:08
 * mail   hl_0074@sina.com
 * </pre>
 *
 * @author lion
 */
@ViewType("resource")
public class ResourceView extends FSResourceView {

	public static final String VIEW_TYPE = "resource";

	public ResourceView() {

	}

	public ResourceView(String name, String path) {

		super(name, path);
	}

	@Override
	public void render(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String path = ParameterizedPath.mapPath(this.path, new RequestModel(request));
		String filePath = FilenameUtils.separatorsToUnix(AppPaths.WEBROOT + "/" + path);
		try {
			sendfile(request, response, new File(filePath));
		} catch (WebException e) {
			throw e;
		} catch (Exception e) {
			throw new WebException(500, e.getMessage());
		}
	}

	@Override
	public String type() {

		return VIEW_TYPE;
	}

}
